import '../entities/user_history_item_entity.dart';

/// 用户历史记录响应模型
/// 
/// 参考小程序实现：wanzhende-m/miniprogram/utils/api.js getUserHistory
class UserHistoryResp {
  final String status;
  final String? message;
  final List<UserHistoryItemEntity> data;

  UserHistoryResp({
    required this.status,
    this.message,
    required this.data,
  });

  /// 从JSON创建响应对象
  factory UserHistoryResp.fromJson(Map<String, dynamic> json) {
    final dataList = json['data'] as List<dynamic>?;
    
    return UserHistoryResp(
      status: json['status']?.toString() ?? 'error',
      message: json['message']?.toString(),
      data: dataList != null
          ? dataList
              .map((item) => UserHistoryItemEntity.fromJson(item as Map<String, dynamic>))
              .toList()
          : [],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'status': status,
      if (message != null) 'message': message,
      'data': data.map((item) => item.toJson()).toList(),
    };
  }

  /// 创建成功响应
  factory UserHistoryResp.success(List<UserHistoryItemEntity> data, {String? message}) {
    return UserHistoryResp(
      status: 'success',
      message: message,
      data: data,
    );
  }

  /// 创建失败响应
  factory UserHistoryResp.failure(String message) {
    return UserHistoryResp(
      status: 'error',
      message: message,
      data: [],
    );
  }

  /// 是否成功
  bool get isSuccess => status == 'success';

  /// 是否失败
  bool get isError => status == 'error';

  /// 历史记录数量
  int get count => data.length;

  /// 是否为空
  bool get isEmpty => data.isEmpty;

  /// 是否有数据
  bool get isNotEmpty => data.isNotEmpty;
}

